package com.zyy.smartcity1129.citysubway;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.SubwayMainInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.Comparator;
import java.util.List;

/**
 * @ClassName: CitySubway$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 3/12/2021$
 * @Version: 1.0
 */
public class CitySubwayFragment extends BaseFragment {
    private RecyclerView rvOnly;
    private Button btnNext;
    public static SubwayMainInfo.DataBean subway;

    @Override
    protected int layId() {
        return R.layout.f_rv_only;
    }

    @Override
    protected String title() {
        return "建国门站";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        rvOnly.setLayoutManager(new LinearLayoutManager(getContext()));
        th(() -> {
            List<SubwayMainInfo.DataBean> subwayList = Http.getInstance().get("/prod-api/api/metro/list?currentName=建国门", null, SubwayMainInfo.class, 1).getData();
            subwayList.sort(Comparator.comparing(SubwayMainInfo.DataBean::getReachTime));
            RvAd<SubwayMainInfo.DataBean> rvAd = new RvAd<SubwayMainInfo.DataBean>(subwayList, R.layout.rv_subway_main) {
                private void initView(View vi) {
                    tvSubLineName = (TextView) vi.findViewById(R.id.tvSubLineName);
                    tvSubNextStep = (TextView) vi.findViewById(R.id.tvSubNextStep);
                    tvSubReachTime = (TextView) vi.findViewById(R.id.tvSubReachTime);
                    viewSub1 = (View) vi.findViewById(R.id.viewSub1);
                }

                private View viewSub1;
                private TextView tvSubReachTime;
                private TextView tvSubNextStep;
                private TextView tvSubLineName;

                @Override
                protected void initR(View vi, SubwayMainInfo.DataBean dataBean, int p) {
                    initView(vi);
                    tvSubLineName.setText(dataBean.getLineName());
                    tvSubNextStep.setText("下一站：" + dataBean.getNextStep().getName());
                    tvSubReachTime.setText("到达本站时长：" + dataBean.getReachTime() + "min");
                    if (subwayList.size() == p) {
                        viewSub1.setBackground(ContextCompat.getDrawable(getContext(), R.color.white));
                    }
                    tvSubLineName.setOnClickListener(view -> {
                        subway = dataBean;
                        add(new Subway1Fragment());
                    });
                }
            };
            ui(() -> {
                rvOnly.setAdapter(rvAd);
            });
        });
    }

    private void initView(View iv) {
        rvOnly = (RecyclerView) iv.findViewById(R.id.rvOnly);
        btnNext = (Button) iv.findViewById(R.id.btnNext);
    }
}
